package com.enki.cigarettes.controller;

import com.enki.cigarettes.entity.BannerT;
import com.enki.cigarettes.handler.Response;
import com.enki.cigarettes.repository.BannerTRepository;
import com.enki.cigarettes.statics.StaticKeys;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author WCS.Wang
 * @version V1.0
 * @Package com.enki.cigarettes.controller
 * @Name e-cigarettes-shop-api-module
 * @Description: TODO banners
 * @date 2019-08-02
 */
@RestController
@RequestMapping("/api/banners")
@Slf4j
public class BannerController {

    @Autowired
    BannerTRepository bannerTRepository;

    @ApiOperation(value = "活动数据")
    @GetMapping
    public Object findList() {
        try {
            List<BannerT> beanList = bannerTRepository.findByRemoved(StaticKeys.REMOVED);
            return Response.success(beanList, StaticKeys.SUCCESS);
        } catch (Exception e) {
            log.error("获取活动数据失败：" + e.getMessage());
            e.printStackTrace();
            return Response.serverError_500("获取活动数据失败");
        }
    }


    @ApiOperation(value = "活动数据")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    @GetMapping(value = "/{id}")
    public Object findOne(@PathVariable String id) {
        try {
            BannerT bean = bannerTRepository.findOneByIdAndRemoved(id, StaticKeys.REMOVED);
            if(bean == null) {
                return Response.notFound_404("不存在的活动信息");
            }
            return Response.success(bean, StaticKeys.SUCCESS);
        } catch (Exception e) {
            log.error("获取活动数据失败：" + e.getMessage());
            e.printStackTrace();
            return Response.serverError_500("获取活动数据失败");
        }
    }

}
